package Page.LoginInterfacePage;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;

import java.net.URL;
import java.util.ResourceBundle;

public class LoginInterfaceController implements Initializable {
    @FXML
    public Button managerButton;
    @FXML
    private TextField accountTextField;
    @FXML
    private TextField passwordTextField;

    private LoginInterfaceViewModel viewModel;

    public void initialize(URL location, ResourceBundle resources) {
        this.viewModel = new LoginInterfaceViewModel();

        // 绑定视图
        accountTextField.textProperty().bindBidirectional(this.viewModel.getUserIdProperty());
        passwordTextField.textProperty().bindBidirectional(this.viewModel.getPasswordProperty());

        // 绑定监听器
        accountTextField.textProperty().addListener((observable, oldValue, newValue) -> {
            this.viewModel.updateUser(newValue);
        });
        passwordTextField.textProperty().addListener((observable, oldValue, newValue) -> {
            this.viewModel.updatePassword(newValue);
        });

        managerButton.textProperty().bindBidirectional(viewModel.getButtonText());

        // 管理员不需要名字
        viewModel.ifManagerProperty().addListener((observable, oldValue, newValue) -> {
            accountTextField.setDisable(newValue);
        });
    }

    public void backButtonClicked(javafx.event.ActionEvent actionEvent) {
        this.viewModel.backButtonClicked();
    }

    public void loginButtonClicked(javafx.event.ActionEvent actionEvent) {
        this.viewModel.loginButtonClicked();
    }
    public void managerButtonClicked(javafx.event.ActionEvent actionEvent) {
        this.viewModel.managerLoginButtonCLicked();
    }
}